<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
        ref="form"
      ></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser" size="primary"
        >+ 新增</el-button
      >
      <el-upload
        class="upload-demo"
        action="http://127.0.0.1:8888/api/pub/v2/pa/importExcel"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="1"
        :on-success="onSuccess"
        :on-error="OnError"
        :on-exceed="handleExceed"
        :file-list="fileList"
      >
        <el-button size="primary" type="success">批量导入</el-button>
      </el-upload>
      <common-form inline :formLabel="formLabel" :form="searchFrom">
        <el-button
          type="primary"
          @click="getList(searchFrom.keyword)"
          size="primary"
          >搜索</el-button
        >
      </common-form>
    </div>
    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getList()"
      @edit="editUser"
      @del="delUser"
    ></common-table>
  </div>
</template>

<script>
import CommonForm from "@/components/CommonForm";
import CommonTable from "@/components/CommonTable";
import { selectByPage } from "@/api/getPaData.js";
import { insert } from "@/api/getPaData.js";
import { deleteById } from "@/api/getPaData.js";
import { update } from "@/api/getPaData.js";
import { selectByNameOrKey } from "@/api/getPaData.js";

export default {
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      operateType: "add",
      isShow: false,
      tableData: [],
      tableLabel: [
        {
          prop: "entrustedCaseNumber",
          label: "委案序号",
          required: true,
        },
        {
          prop: "name",
             label: "客户名称",
          required: true,
        },
        {
          prop: "contractNumber",
          label: "合同号",
          required: true,
        },
        {
          prop: "cardNum",
          label: "身份证",
          required: true,
        },
        {
          prop: "sex",
          label: "性别",
          required: true,
        },
        {
          prop: "birthDate",
          label: "出生日期",
          required: true,

        },
        {
          prop: "homeAddress",
          label: "身份证住址",
          required: true,
        },
        {
          prop: "loanNum",
          label: "贷款金额",
          required: true,
        },
        {
          prop: "litigationPrincipal",
          label: "诉讼本金",
          required: true,
        },
        {
          prop: "interestArrears",
          label: "拖欠利息（含罚息）",
          required: true,
        },
        {
          prop: "compoundInterestArrears",
          label: "拖欠复利",
          required: true,
        },
        {
          prop: "litigationInterest",
          label: "诉讼利息",
          required: true,
        },
        {
          prop: "litigationAmount",
          label: "诉讼标的",
          required: true,
        },
        {
          prop: "rateMonth",
          label: "月利率",
          required: true,
        },
        {
          prop: "rateYear",
          label: "年利率",
          required: true,
        },
        {
          prop: "loanPeriod",
          label: "贷款期限",
          required: true,
        },
        {
          prop: "contractDate",
          label: "合同签订日期",
          required: true,
        },
        {
          prop: "expiredDate",
          label: "到期日",
          required: true,
        },
        {
          prop: "launchedDate",
          label: "发放日期",
          required: true,
        },
        {
          prop: "lineCredit",
          label: "授信额度",
          required: true,
        },
        {
          prop: "lastPaidDate",
          label: "最后一次足额还本息日期",
          required: true,
        },
        {
          prop: "calculatedPaidInterest",
          label: "除去已足额偿还本息后的已还利息",
          required: true,
        },
        {
          prop: "tel",
          label: "手机号码",
          required: true,
        },
        {
          prop: "loanUse",
          label: "贷款用途",
          required: true,
        },
        {
          prop: "payWay",
          label: "还款方式",
          required: true,
        },
  
        {
          prop: "formerNameContract",
          label: "贷款合同前称",
          required: true,
        },
        {
          prop: "verifiedDocumentAddress",
          label: "送达地址确认书地址",
          required: true,
        },
        {
          prop: "deadlineDate",
          label: "截止日期",
          required: true,
        },
        {
          prop: "startDateInterestAfterJudgment",
          label: "判决后利息起始日期",
          required: true,
        },
        {
          prop: "firstOverdueDate",
          label: "首次逾期日",
          required: true,
        },
        {
          prop: "paidPrincipal",
          label: "已还本金",
          required: true,
        },
        {
          prop: "paidInterest",
          label: "已还利息",
          required: true,
        },
        {
          prop: "paymentWay",
          label: "支付方式",
          required: true,
        },
        {
          prop: "lawsuitNum",
          label: "诉讼案号",
          required: true,
        },
        {
          prop: "courtDate",
          label: "开庭时间",
          required: true,
        },
        {
          prop: "litigationFee",
          label: "诉讼费",
          required: true,
        },
        {
          prop: "halfLitigationFee",
          label: "诉讼费减半",
          required: true,
        },
        {
          prop: "litigationDate",
          label: "立案日期",
          required: true,
        },
        {
          prop: "securityFee",
          label: "保全费",
          required: true,
        },
        {
          prop: "securityNum",
          label: "保全案号",
          required: true,
        },
        {
          prop: "executeDate",
          label: "执行立案时间",
          required: true,
        },
        {
          prop: "executeNum",
          label: "执行案号",
          required: true,
        },
        {
          prop: "cardNumDistrict",
          label: "身份证住址所属行政区域",
          required: true,
        },
        {
          prop: "seizeAddress",
          label: "查封地址",
          required: true,
        },
        {
          prop: "declaredFee",
          label: "公告费",
          required: true,
        },
        {
          prop: "lawyerFee",
          label: "律师费",
          required: true,
        },
        {
          prop: "executeAmount",
          label: "执行标的",
          required: true,
        },

        {
          prop: "executeFee",
          label: "执行费",
          required: true,
        },
        {
          prop: "executeAll",
          label: "执行合计",
          required: true,
        },
        {
          prop: "executeJudge",
          label: "执行承办法官",
          required: true,
        },
        {
          prop: "fee",
          label: "冻结存款",
          required: true,

        }


     
      ],
      config: {
        page: 1,
        total: 20,
        loading: false,
      },
      operateForm: {
        
    
           id:"",
           name:"",
           entrustedCaseNumber:"",
           contractNumber:"", 
           cardNum:"",
           sex:"",    
           birthDate:"",
           homeAddress:"",
           loanNum:"",
           litigationPrincipal:"",
           interestArrears:"",
           compoundInterestArrears:"",
           litigationInterest:"",  
           litigationAmount:"", 
           rateMonth:"",
           rateYear:"",
           loanPeriod:"",
           contractDate:"",
           expiredDate:"",
           launchedDate:"",
           lineCredit:"",
           lastPaidDate:"",
           calculatedPaidInterest:"",
            tel:"",
           loanUse:"",
           payWay:"",
           payDay:"",
           formerNameContract:"",
           verifiedDocumentAddress:"",
           deadlineDate:"", 
           startDateInterestAfterJudgment:"",
           firstOverdueDate:"",
           paidPrincipal:"",
           paidInterest:"",
           paymentWay:"",
           lawsuitNum:"",  
           courtDate:"",  
           litigationFee:"",
           halfLitigationFee:"",
           litigationDate:"",
           securityFee:"",  
           securityNum:"",
           executeDate:"",
           executeNum:"", 
           cardNumDistrict:"",
           seizeAddress:"",
           declaredFee:"",
           lawyerFee:"",
           executeAmount:"",
           executeFee:"",
           executeAll:"",
           executeJudge:"",
           fee:""
 
      },
      operateFormLabel: [
                  {
          model: "entrustedCaseNumber",
          label: "委托序号",
          
        },
        {
          model: "name",
          label: "客户名称",

        },
        {
          model: "contractNumber",
          label: "合同号",
          
        },
        {
          model: "cardNum",
          label: "身份证",
          
        },
        {
          model: "sex",
          label: "性别",
             type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
          ],
          
        },
        {
          model: "birthDate",
          label: "出生日期",
                 type: "date"
          
        },
        {
          model: "homeAddress",
          label: "身份证住址",
          
        },
        {
          model: "loanNum",
          label: "贷款金额",
          
        },
        {
          model: "litigationPrincipal",
          label: "诉讼本金",
          
        },
        {
          model: "interestArrears",
          label: "拖欠利息（含罚息）",
          
        },
        {
          model: "compoundInterestArrears",
          label: "拖欠复利",
          
        },
        {
          model: "litigationInterest",
          label: "诉讼利息",
          
        },
        {
          model: "litigationAmount",
          label: "诉讼标的",
          
        },
        {
          model: "rateMonth",
          label: "月利率",
          
        },
        {
          model: "rateYear",
          label: "年利率",
          
        },
        {
          model: "loanPeriod",
          label: "贷款期限",
          
        },
        {
          model: "contractDate",
          label: "合同签订日期",
                 type: "date"
          
        },
        {
          model: "expiredDate",
          label: "到期日",
                 type: "date"
          
        },
        {
          model: "launchedDate",
          label: "发放日期",
                 type: "date"
          
        },
        {
          model: "lineCredit",
          label: "授信额度",
          
        },
        {
          model: "lastPaidDate",
          label: "最后一次足额还本息日期",
                 type: "date"
          
        },
        {
          model: "calculatedPaidInterest",
          label: "除去已足额偿还本息后的已还利息",
          
        },
        {
          model: "tel",
          label: "手机号码",
          
        },
        {
          model: "loanUse",
          label: "贷款用途",
          
        },
        {
          model: "payWay",
          label: "还款方式",
          
        },
  
        {
          model: "formerNameContract",
          label: "贷款合同前称",
          
        },
        {
          model: "verifiedDocumentAddress",
          label: "送达地址确认书地址",
          
        },
        {
          model: "deadlineDate",
          label: "截止日期",
                 type: "date"
          
        },
        {
          model: "startDateInterestAfterJudgment",
          label: "判决后利息起始日期",
                 type: "date"
          
        },
        {
          model: "firstOverdueDate",
          label: "首次逾期日",
                 type: "date"
          
        },
        {
          model: "paidPrincipal",
          label: "已还本金",
          
        },
        {
          model: "paidInterest",
          label: "已还利息",
          
        },
        {
          model: "paymentWay",
          label: "支付方式",
          
        },
        {
          model: "lawsuitNum",
          label: "诉讼案号",
          
        },
        {
          model: "courtDate",
          label: "开庭时间",
          
        },
        {
          model: "litigationFee",
          label: "诉讼费",
          
        },
        {
          model: "halfLitigationFee",
          label: "诉讼费减半",
          
        },
        {
          model: "litigationDate",
          label: "立案日期",
                 type: "date"
          
        },
        {
          model: "securityFee",
          label: "保全费",
          
        },
        {
          model: "securityNum",
          label: "保全案号",
          
        },
        {
          model: "executeDate",
          label: "执行立案时间",
                 type: "date"
          
        },
        {
          model: "executeNum",
          label: "执行案号",
          
        },
        {
          model: "cardNumDistrict",
          label: "身份证住址所属行政区域",
          
        },
        {
          model: "seizeAddress",
          label: "查封地址",
          
        },
        {
          model: "declaredFee",
          label: "公告费",
          
        },
        {
          model: "lawyerFee",
          label: "律师费",
          
        },
        {
          model: "executeAmount",
          label: "执行标的",
          
        },

        {
          model: "executeFee",
          label: "执行费",
          
        },
        {
          model: "executeAll",
          label: "执行合计",
          
        },
        {
          model: "executeJudge",
          label: "执行承办法官",
          
        },
        {
          model: "fee",
          label: "冻结存款",
          

        }

        
       
      ],
      searchFrom: {
        keyword: "",
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
        },
      ],
    };
  },
  methods: {
    onSuccess()
    {
          this.$message({
          message: '信息录入成功，请刷新页面',
          type: 'success'
        });

    },
    OnError()
    {
      this.$message({
          message: '信息录入失败，请检查日期格式,正确格式为2021/1/19',
          type: 'error'
        });

    },
    async getList(name) {
      this.config.loading = false;
      // 搜索时，页码需要设置为1，才能正确返回数据，因为数据是从第一页开始返回的
      name ? (this.config.page = 1) : "";
      if (this.searchFrom.keyword == "") {
        const result = await selectByPage(this.config.page);
        console.log(result);
        if (result.data.code == 0) {
          this.tableData = result.data.data.paList;
          this.config.total = result.data.data.count;
          this.config.loading = false;
        }
      } else {
        console.log(name);
        const result = await selectByNameOrKey(
          this.searchFrom.keyword,
          this.config.page
        );
        console.log(result);

        this.tableData = result.data.data.paList;
        this.config.total = result.data.data.count;
        this.config.loading = false;
      }
    },
    async SelectByName(name) {
      name ? (this.config.page = 1) : "";
      console.log(name);
      const result = await selectByNameOrKey(name, this.config.page);
      console.log(result);

      this.tableData = result.data.data.paList;
      this.config.total = result.data.data.count;
      this.config.loading = false;
    },
    addUser() {
      this.operateForm = {};
      this.operateType = "add";
      this.isShow = true;
    },
    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateForm = row;
    },
    confirm() {
      if (this.operateType === "edit") {
        this.operateForm.sex === 1
          ? (this.operateForm.sex = "男")
          : (this.operateForm.sex = "女");
        update(
 

  
           this.operateForm.id,
           this.operateForm.entrustedCaseNumber,
           this.operateForm.name,
           this.operateForm.contractNumber, 
           this.operateForm.cardNum,
           this.operateForm.sex,    
           this.operateForm.birthDate,
           this.operateForm.homeAddress,
           this.operateForm.loanNum,
           this.operateForm.litigationPrincipal,
           this.operateForm.interestArrears,
           this.operateForm.compoundInterestArrears,
           this.operateForm.litigationInterest,  
           this.operateForm.litigationAmount, 
           this.operateForm.rateMonth,
           this.operateForm.rateYear,
           this.operateForm.loanPeriod,
           this.operateForm.contractDate,
           this.operateForm.expiredDate,
           this.operateForm.launchedDate,
          this.operateForm.lineCredit,
           this.operateForm.lastPaidDate,
           this.operateForm.calculatedPaidInterest,
            this.operateForm.tel,
           this.operateForm.loanUse,
           this.operateForm.payWay,
           this.operateForm.payDay,
           this.operateForm.formerNameContract,
           this.operateForm.verifiedDocumentAddress,
           this.operateForm.deadlineDate, 
           this.operateForm.startDateInterestAfterJudgment,
           this.operateForm.firstOverdueDate,
           this.operateForm.paidPrincipal,
           this.operateForm.paidInterest,
           this.operateForm.paymentWay,
           this.operateForm.lawsuitNum,  
           this.operateForm.courtDate,  
           this.operateForm.litigationFee,
           this.operateForm.halfLitigationFee,
           this.operateForm.litigationDate,
           this.operateForm.securityFee,  
           this.operateForm.securityNum,
           this.operateForm.executeDate,
           this.operateForm.executeNum, 
           this.operateForm.cardNumDistrict,
           this.operateForm.seizeAddress,
           this.operateForm.declaredFee,
           this.operateForm.lawyerFee,
           this.operateForm.executeAmount,
           this.operateForm.executeFee,
           this.operateForm.executeAll,
           this.operateForm.executeJudge,
           this.operateForm.fee
 
        ).then((res) => {
          (this.isShow = false),
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
          this.getList();
        });
      } else {
        // this.$http.post('/api/user/add', this.operateForm).then(res => {
        console.log(this.operateForm);
        // name, sex, nature, address, card_num, lawyer_id
        this.operateForm.sex === 1
          ? (this.operateForm.sex = "男")
          : (this.operateForm.sex = "女");

       insert(
           
     
           
           this.operateForm.entrustedCaseNumber,
                 this.operateForm.name,
           this.operateForm.contractNumber, 

           this.operateForm.cardNum,
           this.operateForm.sex,    
           this.operateForm.birthDate,
           this.operateForm.homeAddress,
           this.operateForm.loanNum,
           this.operateForm.litigationPrincipal,
           this.operateForm.interestArrears,
           this.operateForm.compoundInterestArrears,
           this.operateForm.litigationInterest,  
           this.operateForm.litigationAmount, 
           this.operateForm.rateMonth,
           this.operateForm.rateYear,
           this.operateForm.loanPeriod,
           this.operateForm.contractDate,
           this.operateForm.expiredDate,
           this.operateForm.launchedDate,
          this.operateForm.lineCredit,
           this.operateForm.lastPaidDate,
           this.operateForm.calculatedPaidInterest,
            this.operateForm.tel,
           this.operateForm.loanUse,
           this.operateForm.payWay,
           this.operateForm.payDay,
           this.operateForm.formerNameContract,
           this.operateForm.verifiedDocumentAddress,
           this.operateForm.deadlineDate, 
           this.operateForm.startDateInterestAfterJudgment,
           this.operateForm.firstOverdueDate,
           this.operateForm.paidPrincipal,
           this.operateForm.paidInterest,
           this.operateForm.paymentWay,
           this.operateForm.lawsuitNum,  
           this.operateForm.courtDate,  
           this.operateForm.litigationFee,
           this.operateForm.halfLitigationFee,
           this.operateForm.litigationDate,
           this.operateForm.securityFee,  
           this.operateForm.securityNum,
           this.operateForm.executeDate,
           this.operateForm.executeNum, 
           this.operateForm.cardNumDistrict,
           this.operateForm.seizeAddress,
           this.operateForm.declaredFee,
           this.operateForm.lawyerFee,
           this.operateForm.executeAmount,
           this.operateForm.executeFee,
           this.operateForm.executeAll,
           this.operateForm.executeJudge,
           this.operateForm.fee
 
        ).then((res) => {
          if (res.data.code === 0) {
            console.log("插入成功");
            this.$message({
              type: "success",
              message: "添加成功!",
            });
          } else {
            console.log("插入失败");
            this.$message({
              type: "error",
              message: "添加失败!",
            });
          }
          (this.isShow = false), this.getList();
        });
      }
    },

    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = row.id;
          // this.$http
          //   .get('/api/v1/pub/defendant_one/deleteById', {
          //     params: {
          //       id
          //     }
          //   })
          const result = deleteById(id).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>


<style lang="scss" scoped>
.manage {
  height: 90%;
  padding-bottom: 20px;
  padding-top: 20px;
  overflow: hidden;
  &-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
  }
}
</style>